@echo off
REM ==================================================
REM KWDB Migration Tool Startup Script - Windows Version
REM ==================================================

setlocal EnableDelayedExpansion

REM 设置应用程序标题
title MySQL to KWDB 迁移工具 v1.0.0

REM 设置颜色
color 0A

echo.
echo ==================================================
echo    MySQL to KWDB Migration tools
echo    version: 1.0.0
echo ==================================================
echo.

REM 检查Java环境 - 优先使用内置JRE
set LOCAL_JAVA=%~dp0jre\bin\java.exe
if exist "%LOCAL_JAVA%" (
    set JAVA_CMD=%LOCAL_JAVA%
) else (
    set JAVA_CMD=java
)

REM 检查Java环境
echo [INFO] Check the Java environment...
%JAVA_CMD% -version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] The Java runtime environment is not found!
    echo [INFO] Please make sure JRE 1.8 is available
    pause
    exit /b 1
)

REM 获取Java版本
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
    set JAVA_VERSION=%%g
)
echo [INFO] Java version: %JAVA_VERSION%

REM 检查Java版本是否支持
echo %JAVA_VERSION% | findstr "1.8" >nul
if %errorlevel% equ 0 (
    echo [INFO] Java version check passes
) else (
    echo %JAVA_VERSION% | findstr "9." >nul
    if %errorlevel% equ 0 (
        echo [INFO] Java version check passes
    ) else (
        echo %JAVA_VERSION% | findstr "10." >nul
        if %errorlevel% equ 0 (
            echo [INFO] Java version check passes
        ) else (
            echo %JAVA_VERSION% | findstr "11." >nul
            if %errorlevel% equ 0 (
                echo [INFO] The Java version check passes
            ) else (
                echo %JAVA_VERSION% | findstr "12." >nul
                if %errorlevel% equ 0 (
                    echo [INFO] Java version check passes
                ) else (
                    echo [WARN] Unverified Java version, may not be compatible
                )
            )
        )
    )
)

REM 设置应用程序目录
set APP_HOME=%~dp0
if "%APP_HOME%"=="" set APP_HOME=%CD%\
set APP_HOME=%APP_HOME:~0,-1%

echo [INFO] Application Directory: %APP_HOME%

REM 设置类路径
set CLASSPATH=%APP_HOME%\target\kwdb-import-tool-1.0.0.jar
set CLASSPATH=%CLASSPATH%;%APP_HOME%\target\lib\*

REM 检查JAR文件是否存在
if not exist "%APP_HOME%\target\kwdb-import-tool-1.0.0.jar" (
    echo [ERROR] The main JAR file was not found: target\kwdb-import-tool-1.0.0.jar
    echo [INFO] Build your project using Maven first: mvn clean package
    pause
    exit /b 1
)

REM 检查依赖库目录
if not exist "%APP_HOME%\target\lib" (
    echo [WARN] If the dependency directory is not found, try running the JAR directly...
    set CLASSPATH=%APP_HOME%\target\kwdb-import-tool-1.0.0.jar
)

REM 设置日志目录
if not exist "%APP_HOME%\logs" (
    echo [INFO] Create a log directory...
    mkdir "%APP_HOME%\logs"
)

REM 设置输出目录
if not exist "%APP_HOME%\output" (
    echo [INFO] Create an output directory...
    mkdir "%APP_HOME%\output"
)

REM 设置备份目录
if not exist "%APP_HOME%\backup" (
    echo [INFO] Create a backup catalog...
    mkdir "%APP_HOME%\backup"
)

REM 设置临时目录
if not exist "%APP_HOME%\temp" (
    echo [INFO] Create a temporary catalog...
    mkdir "%APP_HOME%\temp"
)

REM 专门针对5GB SQL文件优化的JVM参数
echo [INFO] Configuring JVM for 5GB SQL file processing...

REM 固定内存设置，针对5GB SQL文件优化
set DEFAULT_JVM_OPTS=-Xms2048m -Xmx6144m -Dfile.encoding=UTF-8

REM 性能优化参数 - 针对大文件处理优化
set PERFORMANCE_OPTS=-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:ParallelGCThreads=6 -XX:ConcGCThreads=3

REM 大文件处理专用优化
set LARGE_FILE_OPTS=-XX:+UseStringDeduplication -XX:+OptimizeStringConcat -XX:+ExplicitGCInvokesConcurrent -XX:+PerfDisableSharedMem

REM 内存和IO优化
set MEMORY_OPTS=-XX:MaxMetaspaceSize=512m -XX:ReservedCodeCacheSize=256m

REM 应用程序参数
set APP_OPTS=-Dapp.home="%APP_HOME%" -Dlog4j.configurationFile="%APP_HOME%/config/log4j2.xml" -Djava.io.tmpdir="%APP_HOME%/temp" -Djava.awt.headless=false

REM 显示内存配置信息
echo [INFO] Memory configuration for 5GB SQL file processing:
echo [INFO] Initial Heap: 2GB, Max Heap: 6GB
echo [INFO] This should handle 5GB SQL files efficiently

REM 如果没有设置JVM参数，使用默认值
if "%JVM_OPTS%"=="" (
    set JVM_OPTS=%DEFAULT_JVM_OPTS%
)

REM 合并所有JVM参数
set FINAL_JVM_OPTS=%JVM_OPTS% %PERFORMANCE_OPTS% %LARGE_FILE_OPTS% %MEMORY_OPTS% %APP_OPTS%

REM 显示启动信息
echo.
echo [INFO] Startup parameters:
echo [INFO] JVM parameters: %FINAL_JVM_OPTS%
echo [INFO] Classpath: %CLASSPATH%
echo.

REM 检查运行模式
set MODE=%1
if "%MODE%"=="" set MODE=--gui

REM 根据参数选择运行模式
if "%MODE%"=="--cli" (
    echo [INFO] Start command line mode...
    set MAIN_CLASS=com.kwdb.importtool.Main
) else if "%MODE%"=="--gui" (
    echo [INFO] Launch the graphical interface mode...
    set MAIN_CLASS=com.kwdb.importtool.Main
) else if "%MODE%"=="--help" (
    echo [INFO] Show help information...
    set MAIN_CLASS=com.kwdb.importtool.Main
) else (
    echo [INFO] Automatic selection of the operating mode...
    set MAIN_CLASS=com.kwdb.importtool.Main
)

REM 检查是否是大文件模式
echo %* | findstr /i "large" >nul
if %errorlevel% equ 0 (
    echo [INFO] Large file mode detected, applying additional optimizations...
    set FINAL_JVM_OPTS=%FINAL_JVM_OPTS% -Dlarge.file.mode=true
)

REM 构建完整的命令行
set CMD=java %FINAL_JVM_OPTS% -cp "%CLASSPATH%" %MAIN_CLASS% %*

echo [INFO] Execute the command: java %FINAL_JVM_OPTS% -cp "..." %MAIN_CLASS% %*
echo.

REM 启动应用程序
echo [INFO] Launch the app...
%CMD%

REM 检查退出代码
set EXIT_CODE=%errorlevel%
echo.
echo [INFO] App exit，code: %EXIT_CODE%

REM 根据退出代码显示相应信息
if %EXIT_CODE% equ 0 (
    echo [INFO] The application exits normally
) else if %EXIT_CODE% equ 1 (
    echo [ERROR] The app quits with an error
) else if %EXIT_CODE% equ 2 (
    echo [INFO] The user cancels the action
) else if %EXIT_CODE% equ 130 (
    echo [INFO] The application is interrupted (Ctrl+C)
) else if %EXIT_CODE% equ 137 (
    echo [ERROR] Out of Memory Error - Java heap space
    echo [SUGGESTION] Try increasing -Xmx parameter or split the SQL file
) else (
    echo [WARN] The application exits abnormally，code: %EXIT_CODE%
)

REM 暂停以便查看输出
if "%MODE%"=="--cli" (
    echo.
    echo Press any key to exit...
    pause >nul
)

endlocal
exit /b %EXIT_CODE%